package com.wu.pojo.dto;

import com.wu.pojo.bean.QuestionAnswerBean;
import com.wu.pojo.po.paperPo.Answer;
import com.wu.utils.CastUtil;
import com.wu.utils.JwtUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.shiro.authc.AuthenticationException;


import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@ApiModel("提交答卷DTO")
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class PaperAnswerDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("问卷id")
    @NotNull(message = "问卷id不能为空")
    private String paperId;

    @ApiModelProperty("机构名称")
    @NotNull(message = "userInfo不能为空")
    private String userInfo;

    @ApiModelProperty("答案列表")
    @NotNull(message = "答案列表不能为空")
    private List<QuestionAnswerBean> answers;

    private String userId;

    public List<Answer> answerListInit() { //初始化参数
        String userId = null;
        try {
            userId = (String)JwtUtils.getClaimByToken(userInfo).get("userId");
        } catch (Exception e) {
            throw new AuthenticationException("token失效");
        }
        this.userId = userId;
        List<Answer> answerList = new ArrayList<>();
        for (QuestionAnswerBean questionAnswer : answers) {
            Answer answer = new Answer();
            if (questionAnswer.getQuestionType() == 2) {   //多选题，遍历答案
                List<String> list = CastUtil.castList(questionAnswer.getAnswerContents(), String.class);//类型转换

                //统一格式
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("[");
                for (int i = 0; i < list.size(); i++) {
                    stringBuilder.append("\"");
                    stringBuilder.append(list.get(i));
                    stringBuilder.append("\"");
                    if (i < list.size() - 1) {
                        stringBuilder.append(",");
                    }
                }
                stringBuilder.append("]");

                answer.setUserId(userId).setPaperId(paperId).setQuestionId(questionAnswer.getQuestionId())
                        .setQuestionType(questionAnswer.getQuestionType())
                        .setAnswerContents(stringBuilder.toString());
                answerList.add(answer);

            } else { //否则为单选或问答题

                //统一格式
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("[").append("\"")
                        .append(questionAnswer.getAnswerContents().toString())
                        .append("\"").append("]");

                answer.setUserId(userId).setPaperId(paperId).setQuestionId(questionAnswer.getQuestionId())
                        .setQuestionType(questionAnswer.getQuestionType())
                        .setAnswerContents(stringBuilder.toString());
                answerList.add(answer);
            }
        }
        return answerList;
    }


}
